/**
 * jims
 */
package com.jims.master.modules.sync.service;

import com.jims.master.common.service.CrudService;
import com.jims.master.modules.sync.dao.ClinicItemDictDao;
import com.jims.master.modules.sync.dao.ExamItemDao;
import com.jims.master.modules.sync.entity.ClinicItemDict;
import com.jims.master.modules.sync.entity.ExamApply;
import com.jims.master.modules.sync.entity.ExamItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查申请明细Service
 * @author 陈晓阳
 * @version 2017-03-14
 */
@Service
@Transactional(readOnly = true)
public class ExamItemService extends CrudService<ExamItemDao, ExamItem> {

    @Autowired
    private ExamItemDao examItemDao;
    @Autowired
    private ClinicItemDictDao clinicItemDictDao;//诊疗项目Dao


    @Transactional(readOnly = false)
    public int save(ExamItem examItem) {
        return super.save(examItem);
    }

    @Transactional(readOnly = false)
    public int saveTj(ExamItem examItem) {
        return dao.insert(examItem);
    }


    /**
     * 根据申请主表Id删除明细
     * @param applyId
     * @return
     * @author 陈晓阳
     */
    @Transactional(readOnly = false)
    public int deleteByApplyId(String applyId) {
        return this.examItemDao.deleteByApplyId(applyId);
    }
    public List findAllItems(String applyId){
        return this.examItemDao.findAllItems(applyId);
    }

    public List<ExamItem> getByApplyId(String applyId){
        return this.examItemDao.getByApplyId(applyId);
    }
    public int delItem( String applyId){
        return dao.delItem(applyId);
    }

    /**
     * 保存病人申请检查子表记录多条数据
     * @return
     * @Author 陈晓阳
     */
    @Transactional(readOnly = false)
    public int saveExamItems(ExamApply examApply, String clincItemId){
        String[] clincItemIds=clincItemId.split(",");
        for(String clinicItemId:clincItemIds){
            String[] itemType=clinicItemId.split("-");
            ClinicItemDict clinicItemDict = new ClinicItemDict();
            clinicItemDict.setId(itemType[0]);
            clinicItemDict=clinicItemDictDao.getById(clinicItemDict);
            ExamItem saveData=new ExamItem();
            saveData.setApplyId(examApply.getId());
            saveData.setExamApply(examApply);
            saveData.setItemType(itemType[1]);
            saveData.setItemName(clinicItemDict.getItemName());
            saveData.setItemCode(clinicItemDict.getItemCode());
            saveData.setClinicItemId(clinicItemDict.getId());
            logger.debug(saveData.getItemType());
            Double price = this.clinicItemDictDao.findPriceById(clinicItemDict.getId());
            saveData.setCosts((price!=null?price:0) + "");
            saveData.setOrgId(examApply.getOrgId());
           /* Double price=this.clinicItemDictDao.findPriceById(clinicItemDict.getId());
            saveData.setCosts((price!=null?price:0)+"");*/
            int i =this.save(saveData);

        }
        return clincItemIds.length;
    }


}